package com.anxin.act.process.service;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.XML;
import com.alibaba.fastjson.JSON;
import com.anxin.act.process.entity.*;
import com.anxin.act.process.vo.RuProcessVo;
import com.anxin.act.task.entity.ViewRuTask;
import com.anxin.act.utils.ActProcessUtils;
import com.anxin.act.utils.ActUtils;
import com.anxin.common.utils.LogUtils;
import com.anxin.common.utils.StringUtils;
import com.anxin.common.utils.sys.UserUtils;
import com.anxin.createproinst.*;
import com.anxin.importactivityparbypiroutesrv.OSB_BP_SOA_HQ_ImportActivityParByPIRouteSrvClient;
import com.anxin.importactivityparbypiroutesrv.OSB_BP_SOA_HQ_ImportActivityParByPIRouteSrvPortImpl;
import com.anxin.importfinishworkitemsrv.OSB_BP_WFP_HQ_ImportFinishWorkItemSrvClient;
import com.anxin.importrelativedatabatchsrv.OSB_BP_WFP_HQ_ImportRelativeDataBatchSrvClient;
import com.anxin.importreassignworkitemroutesrv.OSB_BP_SOA_HQ_ImportReassignWorkItemRouteSrvClient;
import com.anxin.importterminateproinstsrv.OSB_BP_WFP_HQ_ImportTerminateProInstSrvClient;
import com.anxin.inquiryactivityinstsroutesrv.OSB_BP_SOA_HQ_InquiryActivityInstsRouteSrvClient;
import com.anxin.inquirynextpossibpartroutesrv.OSB_BP_SOA_HQ_InquiryNextPossibPartRouteSrvClient;
import com.anxin.osbProinst.entity.OsbProinst;
import com.anxin.osbProinst.service.OsbProinstService;
import com.anxin.osbProinstInfor.entity.OsbProinstInfor;
import com.anxin.osbProinstInfor.service.OsbProinstInforService;
import com.anxin.runningWorkItemProinst.OSB_BP_WFP_HQ_InquiryRunnigWorkItemsSrvClient;
import com.anxin.runningWorkItemProinst.OUTPUTCOLLECTIONITEM;
import com.anxin.sys.system.utils.ConfigUtils;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.impl.bpmn.behavior.ParallelMultiInstanceBehavior;
import org.activiti.engine.impl.bpmn.behavior.SequentialMultiInstanceBehavior;
import org.activiti.engine.impl.bpmn.behavior.UserTaskActivityBehavior;
import org.activiti.engine.impl.pvm.delegate.ActivityBehavior;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.runtime.ProcessInstance;
import com.anxin.sys.system.entity.SysUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class FlowInterfacesService {
    private String tokenID = ConfigUtils.get("HQ_SOA_TOKEN_ID");
    @Value("${actFlow.endpointIP}")
    private String endpointIP;

    @Autowired
    private OsbProinstService osbProinstService;
    @Autowired
    private ActProcessService actProcessService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private OsbProinstInforService osbProinstInforService;


    /**
     * 接口启动流程
     * @param ruProcessVo
     * @param procIns
     */
    public boolean startProcess(RuProcessVo ruProcessVo,  ProcessInstance procIns,String userList){
        boolean flag=true;
        try {
            LogUtils.info("************************ 启动流程接口 开始*****************************************");
            //启动流程标识 true
            ruProcessVo.setIsFlow("true");
            String endpointUrl=endpointIP+ ProcessEnum.ImportCreateProInstRouteSrv.getName();
            // 调用服务接口
            OutputParameters response = OSB_BP_WFP_HQ_ImportCreateProInstSrvClient.callingInterfaces(ruProcessVo,tokenID,endpointUrl);
            // 输出服务调用结果
            System.out.println(response.getBIZSERVICEFLAG());
            System.out.println(response.getBIZRETURNMESSAGE());
            System.out.println(response.getESBFLAG());
            System.out.println(response.getESBRETURNMESSAGE());
            System.out.println(response.getINSTANCEID());


            OsbProinst osbProinst = new OsbProinst();
            //保存接口返回的流程实例id
            if(response!=null && response.getRESPONSECOLLECTION()!=null
                 &&  response.getRESPONSECOLLECTION().getRESPONSECOLLECTIONITEM()!=null
                 && response.getRESPONSECOLLECTION().getRESPONSECOLLECTIONITEM().size()>0){
                String processinstid = response.getRESPONSECOLLECTION().getRESPONSECOLLECTIONITEM().get(0).getPROCESSINSTID();
                osbProinst.setInstanceId(processinstid);

                //环境变量设置参数
                runtimeService.setVariable(procIns.getProcessInstanceId(),"SOA_PROCESSINSTID",processinstid);
                // 保存系统流程id和流程平台实例id
                OsbProinstInfor osbProinstInfor = new OsbProinstInfor();
                osbProinstInfor.setProcessinstId(procIns.getActivityId());
                osbProinstInfor.setPriKey(processinstid);
                osbProinstInforService.save(osbProinstInfor);

            }
            if("FALSE".equals(response.getESBFLAG()) || "FALSE".equals(response.getBIZSERVICEFLAG())){
                //接口调用不成功 返回false
                flag=false;
            }else{

                //启动时 AA跳 调用接口进行 设置参数 并提交
                flag = setRelativeDataBatch(ActProcessUtils.setParameters(procIns.getProcessInstanceId()), procIns.getProcessInstanceId());

                List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery().processInstanceId(procIns.getProcessInstanceId()).activityType("userTask")
                        .orderByHistoricActivityInstanceStartTime().asc().list();
                List<String> strings = new ArrayList<>();
                for (HistoricActivityInstance historicActivityInstance : list) {
                    if(historicActivityInstance.getEndTime() == null && !StringUtils.equals(historicActivityInstance.getAssignee(),"-1")){
                        strings.add(historicActivityInstance.getAssignee());
                    }
                }
                if (strings != null && strings.size() > 0) {
                    this.setActivityPar(procIns.getProcessInstanceId(),userList);
                }
                //提交当前节点
                if(flag){
                    flag= this.complete(procIns.getProcessInstanceId());
                }
            }
            //保存日志
            osbProinst.setEsbFlag(response.getESBFLAG());
            osbProinst.setEsbReturnCode(response.getBIZRETURNCODE());
            osbProinst.setEsbReturnMessage(response.getBIZRETURNMESSAGE());
            osbProinst.setBizServiceFlag(response.getBIZSERVICEFLAG());
            osbProinst.setBizReturnCode(response.getBIZRETURNCODE());
            osbProinst.setBizReturnMessage(response.getBIZRETURNMESSAGE());
            osbProinstService.save(osbProinst);
            LogUtils.info("************************ 启动流程接口 结束*****************************************");
            // 输出服务调用结果
        }catch (Exception e){
            e.printStackTrace();
            LogUtils.error("启动流程接口异常："+e.getMessage());
            flag=false;
        }
        //如果接口失败了 则进行把启动的流程进行终止掉
        if(!flag && StringUtils.isNotBlank(procIns.getProcessInstanceId())){
            this.stopProcess(procIns.getProcessInstanceId());
        }
        return flag;
    }

    /**
     * 终止流程
     * @param actId
     * @return
     */
    public boolean stopProcess(String actId){
        boolean flag=true;
        try {
            LogUtils.info("************************ 终止流程接口 开始*****************************************");
            Map<String, Object> processVar = ActUtils.getProcessVar(actId);
            if(processVar!=null && processVar.get("SOA_PROCESSINSTID")!=null){
                // 调用服务接口
                String endpointUrl=endpointIP+ ProcessEnum.ImportTerminateProInstRouteSrv.getName();
                // 调用服务接口
                com.anxin.importterminateproinstsrv.OutputParameters response = OSB_BP_WFP_HQ_ImportTerminateProInstSrvClient.
                        callingInterfaces(processVar.get("SOA_PROCESSINSTID").toString(), tokenID, endpointUrl);
                // 输出服务调用结果
                System.out.println(response.getBIZSERVICEFLAG());
                System.out.println(response.getBIZRETURNMESSAGE());
                System.out.println(response.getESBFLAG());
                System.out.println(response.getESBRETURNMESSAGE());
                System.out.println(response.getINSTANCEID());
                if("FALSE".equals(response.getESBFLAG()) || "FALSE".equals(response.getBIZSERVICEFLAG())){
                    //接口调用不成功 返回false
                    flag=false;
                }
                //保存日志
                OsbProinst osbProinst = new OsbProinst();
                osbProinst.setEsbFlag(response.getESBFLAG());
                osbProinst.setEsbReturnCode(response.getBIZRETURNCODE());
                osbProinst.setEsbReturnMessage(response.getBIZRETURNMESSAGE());
                osbProinst.setBizServiceFlag(response.getBIZSERVICEFLAG());
                osbProinst.setBizReturnCode(response.getBIZRETURNCODE());
                osbProinst.setBizReturnMessage(response.getBIZRETURNMESSAGE());
                osbProinstService.save(osbProinst);
            }
            LogUtils.info("************************ 终止流程接口 结束*****************************************");
        }catch (Exception e){
            e.printStackTrace();
            LogUtils.error("终止流程接口异常："+e.getMessage());
            flag=false;
        }
        return flag;
    }

    /**
     * 导入提交任务服务
     * @param actId
     * @return
     */
    public boolean complete(String actId){
        boolean flag=true;
        try {
            LogUtils.info("************************ 导入提交任务服务 开始*****************************************");
            List<Map<String, String>> workIdlist = getProcessInstId(actId);
            if(workIdlist!=null && workIdlist.size()>0){
                //循环工作项ID提交任务
                for (Map<String, String> workId : workIdlist) {
                    if(workId.get("workItemID")==null){
                        continue;
                    }
                    String endpointUrl=endpointIP+ ProcessEnum.ImportFinishWorkItemRouteSrv.getName();
                    // 调用服务接口
                    com.anxin.importfinishworkitemsrv.OutputParameters response = OSB_BP_WFP_HQ_ImportFinishWorkItemSrvClient.
                            callingInterfaces(workId.get("workItemID"), tokenID, endpointUrl);
                    // 输出服务调用结果
                    System.out.println(response.getBIZSERVICEFLAG());
                    System.out.println(response.getBIZRETURNMESSAGE());
                    System.out.println(response.getESBFLAG());
                    System.out.println(response.getESBRETURNMESSAGE());
                    System.out.println(response.getINSTANCEID());
                    if("FALSE".equals(response.getESBFLAG()) || "FALSE".equals(response.getBIZSERVICEFLAG())){
                        //接口调用不成功 返回false
                        flag=false;
                    }
                    //保存日志
                    OsbProinst osbProinst = new OsbProinst();
                    osbProinst.setEsbFlag(response.getESBFLAG());
                    osbProinst.setEsbReturnCode(response.getBIZRETURNCODE());
                    osbProinst.setEsbReturnMessage(response.getBIZRETURNMESSAGE());
                    osbProinst.setBizServiceFlag(response.getBIZSERVICEFLAG());
                    osbProinst.setBizReturnCode(response.getBIZRETURNCODE());
                    osbProinst.setInstanceId(workId.get("workItemID"));
                    osbProinst.setBizReturnMessage(response.getBIZRETURNMESSAGE());
                    osbProinstService.save(osbProinst);
                }
            }else{
                //接口调用不成功 返回false
                flag=false;
                LogUtils.error("************************ 导入提交任务服务 未获取到流程实例ID*****************************************");
            }
            LogUtils.info("************************ 导入提交任务服务 结束*****************************************");
        }catch (Exception e){
            e.printStackTrace();
            LogUtils.error("导入提交任务服务接口异常："+e.getMessage());
            flag=false;
        }
        return flag;
    }
    /**
     * 查询流程运行中工作项服务
     * @param processInstId
     * @return
     */
    public List<Map<String,String>> getProcessInstId(String processInstId){
        ArrayList<Map<String,String>> list=new ArrayList<Map<String,String>>();
        try {
            LogUtils.info("************************ 查询流程运行中工作项服务 开始*****************************************");
            Map<String, Object> processVar = ActUtils.getProcessVar(processInstId);
            if(processVar!=null && processVar.get("SOA_PROCESSINSTID")!=null){
                // 调用服务接口
                String endpointUrl=endpointIP+ ProcessEnum.InquiryRunnigWorkItemsRouteSrv.getName();
                // 调用服务接口
                com.anxin.runningWorkItemProinst.OutputParameters response = OSB_BP_WFP_HQ_InquiryRunnigWorkItemsSrvClient.
                        callingInterfaces(processVar.get("SOA_PROCESSINSTID").toString(), tokenID, endpointUrl);
                // 输出服务调用结果
                System.out.println(response.getBIZSERVICEFLAG());
                System.out.println(response.getBIZRETURNMESSAGE());
                System.out.println(response.getESBFLAG());
                System.out.println(response.getESBRETURNMESSAGE());
                System.out.println(response.getINSTANCEID());

                //保存日志
                OsbProinst osbProinst = new OsbProinst();
                //解析 流程实例ID
                if(response.getOUTPUTCOLLECTION()!=null && response.getOUTPUTCOLLECTION().getOUTPUTCOLLECTIONITEM()!=null
                        && response.getOUTPUTCOLLECTION().getOUTPUTCOLLECTIONITEM().size()>0){
                    for (OUTPUTCOLLECTIONITEM outputcollectionitem : response.getOUTPUTCOLLECTION().getOUTPUTCOLLECTIONITEM()) {
                        if(StringUtils.isNotBlank(outputcollectionitem.getOUT1())){
                            JSONObject jsonObject = XML.toJSONObject(outputcollectionitem.getOUT1());
                            if(jsonObject!=null && jsonObject.get("s:WFWorkItemList")!=null){
                                JSONObject jsonObject2 = (JSONObject)jsonObject.get("s:WFWorkItemList");
                                if(jsonObject2!=null && jsonObject2.get("s:WFWorkItem")!=null){
                                    if(jsonObject2.get("s:WFWorkItem") instanceof JSONArray){
                                        JSONArray jsonArray =jsonObject2.getJSONArray(jsonObject2.get("s:WFWorkItem").toString());
                                        for (Object o : jsonArray) {
                                            com.alibaba.fastjson.JSONObject jsonObject4 = com.alibaba.fastjson.JSONObject.parseObject(JSON.toJSONString(o));
                                            Map<String,String> map = new HashMap<>();
                                            if(jsonObject4!=null && jsonObject4.get("s:workItemID")!=null){
                                                map.put("workItemID",jsonObject4.get("s:workItemID").toString());
                                                map.put("participant",jsonObject4.get("s:participant").toString());
                                            }
                                            list.add(map);
                                        }
                                    }else{
                                        JSONObject jsonObject4 = (JSONObject) jsonObject2.get("s:WFWorkItem");
                                        Map<String,String> map = new HashMap<>();
                                        if(jsonObject4!=null && jsonObject4.get("s:workItemID")!=null){
                                            map.put("workItemID",jsonObject4.get("s:workItemID").toString());
                                            map.put("participant",jsonObject4.get("s:participant").toString());
                                        }
                                        list.add(map);
                                    }

                                }
                                osbProinst.setDataType(jsonObject.toString());
                            }
                        }
                    }
                }
                osbProinst.setEsbFlag(response.getESBFLAG());
                osbProinst.setEsbReturnCode(response.getBIZRETURNCODE());
                osbProinst.setEsbReturnMessage(response.getBIZRETURNMESSAGE());
                osbProinst.setBizServiceFlag(response.getBIZSERVICEFLAG());
                osbProinst.setBizReturnCode(response.getBIZRETURNCODE());
                osbProinst.setInstanceId(processVar.get("SOA_PROCESSINSTID").toString());
                osbProinst.setBizReturnMessage(response.getBIZRETURNMESSAGE());
                osbProinstService.save(osbProinst);
            }
            LogUtils.info("************************ 查询流程运行中工作项服务 结束*****************************************");
        }catch (Exception e){
            e.printStackTrace();
            LogUtils.error("查询流程运行中工作项服务接口异常："+e.getMessage());
        }
        return list;
    }

    /**
     * 导入设置流程上下文参数服务
     * @param map
     * @param processInstId
     * @return
     */
    public boolean setRelativeDataBatch(Map<String, String> map ,String processInstId){
        boolean flag=true;
        try {
            LogUtils.info("************************ 导入设置流程上下文参数服务 开始*****************************************");
            Map<String, Object> processVar = ActUtils.getProcessVar(processInstId);
            if(map!=null && processVar!=null && processVar.get("SOA_PROCESSINSTID")!=null){
                // 调用服务接口
                String endpointUrl=endpointIP+ ProcessEnum.ImportRelativDataBatchRouteSrv.getName();
                // 调用服务接口
                com.anxin.importrelativedatabatchsrv.OutputParameters response = OSB_BP_WFP_HQ_ImportRelativeDataBatchSrvClient.
                        callingInterfaces(map, processVar.get("SOA_PROCESSINSTID").toString(), tokenID, endpointUrl);
                // 输出服务调用结果
                System.out.println(response.getBIZSERVICEFLAG());
                System.out.println(response.getBIZRETURNMESSAGE());
                System.out.println(response.getESBFLAG());
                System.out.println(response.getESBRETURNMESSAGE());
                System.out.println(response.getINSTANCEID());
                if("FALSE".equals(response.getESBFLAG()) || "FALSE".equals(response.getBIZSERVICEFLAG())){
                    //接口调用不成功 返回false
                    flag=false;
                }
                //保存日志
                OsbProinst osbProinst = new OsbProinst();
                osbProinst.setEsbFlag(response.getESBFLAG());
                osbProinst.setEsbReturnCode(response.getBIZRETURNCODE());
                osbProinst.setEsbReturnMessage(response.getBIZRETURNMESSAGE());
                osbProinst.setBizServiceFlag(response.getBIZSERVICEFLAG());
                osbProinst.setBizReturnCode(response.getBIZRETURNCODE());
                osbProinst.setInstanceId(processVar.get("SOA_PROCESSINSTID").toString());
                osbProinst.setBizReturnMessage(response.getBIZRETURNMESSAGE());
                osbProinstService.save(osbProinst);

            }else{
                LogUtils.error("************************ 导入设置流程上下文参数服务 未找到要设计的参数*****************************************");
            }
            LogUtils.info("************************ 导入设置流程上下文参数服务 结束*****************************************");
        }catch (Exception e){
            e.printStackTrace();
            LogUtils.error("导入设置流程上下文参数服务接口异常："+e.getMessage());
            flag=false;
        }
        return flag;
    }


    /**
     * 改派任务
     * @param targetUser 改派后用户
     * @return
     */
    public void reassign(ViewRuTask viewRuTask, SysUser targetUser){
        try {
            List<String> userIds = new ArrayList<>();
            userIds.add(targetUser.getId());
            LogUtils.info("************************ 改派任务 开始*****************************************");
            List<Map<String,String>> workIdlist = getProcessInstId(viewRuTask.getActId());
            if(workIdlist!=null && workIdlist.size()>0){
                String endpointUrl=endpointIP+ ProcessEnum.ImportReassignWorkItemRouteSrv.getName();
                String workId = "";
                if (workIdlist.size()==1){
                    workId = workIdlist.get(0).get("workItemID");
                }else {
                    for (Map<String,String> map : workIdlist) {
                        SysUser userByUserName = UserUtils.getUserByUserName(viewRuTask.getAssignee());
                        if (userByUserName!=null && StringUtils.equals(userByUserName.getDeputyAccountNumber(),map.get("participant"))){
                            workId = map.get("workItemID");
                        }
                    }
                }
                // 调用服务接口
                com.anxin.importreassignworkitemroutesrv.OutputParameters response = OSB_BP_SOA_HQ_ImportReassignWorkItemRouteSrvClient.
                        callingInterfaces(tokenID,endpointUrl,userIds,UserUtils.getUser().getDeputyAccountNumber(),workId );
                // 输出服务调用结果
                System.out.println(response.getBIZSERVICEFLAG());
                System.out.println(response.getBIZRETURNMESSAGE());
                System.out.println(response.getESBFLAG());
                System.out.println(response.getESBRETURNMESSAGE());
                System.out.println(response.getINSTANCEID());

                //保存日志
                OsbProinst osbProinst = new OsbProinst();
                osbProinst.setEsbFlag(response.getESBFLAG());
                osbProinst.setEsbReturnCode(response.getBIZRETURNCODE());
                osbProinst.setEsbReturnMessage(response.getBIZRETURNMESSAGE());
                osbProinst.setBizServiceFlag(response.getBIZSERVICEFLAG());
                osbProinst.setBizReturnCode(response.getBIZRETURNCODE());
                osbProinst.setInstanceId(workId);
                osbProinst.setBizReturnMessage(response.getBIZRETURNMESSAGE());
                osbProinstService.save(osbProinst);

            }
            LogUtils.info("************************ 改派任务 结束*****************************************");
        }catch (Exception e){
            e.printStackTrace();
            LogUtils.error("改派任务服务接口异常："+e.getMessage());
        }
    }

    /**
     * 查询流程活动节点信息
     */
    public List<String> getWFActivityInst(String processInstId){
        ArrayList<String> list=new ArrayList<String>();
        try {
            LogUtils.info("************************ 查询流程活动节点信息 开始*****************************************");
            Map<String, Object> processVar = ActUtils.getProcessVar(processInstId);
            if(processVar!=null && processVar.get("SOA_PROCESSINSTID")!=null){
                //调用服务接口
                String endpointUrl = endpointIP+ProcessEnum.InquiryActivityInstsRouteSrv.getName();
                com.anxin.inquiryactivityinstsroutesrv.OutputParameters response = OSB_BP_SOA_HQ_InquiryActivityInstsRouteSrvClient.callingInterfaces(processVar.get("SOA_PROCESSINSTID").toString(), tokenID, endpointUrl);
                // 输出服务调用结果
                System.out.println(response.getBIZSERVICEFLAG());
                System.out.println(response.getBIZRETURNMESSAGE());
                System.out.println(response.getESBFLAG());
                System.out.println(response.getESBRETURNMESSAGE());
                System.out.println(response.getINSTANCEID());
                //保存日志
                OsbProinst osbProinst = new OsbProinst();
                //解析 流程活动id
                if(response.getOUTPUTCOLLECTION()!=null && response.getOUTPUTCOLLECTION().getOUTPUTCOLLECTIONITEM()!=null
                        && response.getOUTPUTCOLLECTION().getOUTPUTCOLLECTIONITEM().size()>0){
                    for (com.anxin.inquiryactivityinstsroutesrv.OUTPUTCOLLECTIONITEM outputcollectionitem : response.getOUTPUTCOLLECTION().getOUTPUTCOLLECTIONITEM()) {
                        if(StringUtils.isNotBlank(outputcollectionitem.getOUTPUTEXT())){
                            JSONObject jsonObject = XML.toJSONObject(outputcollectionitem.getOUTPUTEXT());
                            if(jsonObject!=null && jsonObject.get("s:WFActivityInstList")!=null){
                                JSONObject jsonObject2 =jsonObject.getJSONObject("s:WFActivityInstList");
                                if(jsonObject2!=null && jsonObject2.get("s:WFActivityInst")!=null){
                                    if(jsonObject2.get("s:WFActivityInst") instanceof JSONArray){
                                        JSONArray jsonArray =jsonObject2.getJSONArray("s:WFActivityInst");
                                        for (Object o : jsonArray) {
                                            com.alibaba.fastjson.JSONObject jsonObject3 = com.alibaba.fastjson.JSONObject.parseObject(JSON.toJSONString(o));
                                            if (jsonObject3 != null && jsonObject3.get("s:activityInstID") != null && jsonObject3.get("s:currentState") != null) {
                                                if (StringUtils.equals(jsonObject3.get("s:currentState").toString(), "2")) {
                                                    list.add(jsonObject3.get("s:activityInstID").toString());
                                                }
                                            }
                                        }
                                    }else {
                                        JSONObject jsonObject3 =jsonObject2.getJSONObject("s:WFActivityInst");
                                        if (jsonObject3 != null && jsonObject3.get("s:activityInstID") != null && jsonObject3.get("s:currentState") != null) {
                                            if (StringUtils.equals(jsonObject3.get("s:currentState").toString(), "2")) {
                                                list.add(jsonObject3.get("s:activityInstID").toString());
                                            }
                                        }
                                    }

                                }
                                osbProinst.setDataType(jsonObject.toString());
                            }
                        }
                    }

                }
            }
            LogUtils.info("************************ 查询流程活动节点信息服务 结束*****************************************");
        }catch (Exception e){
            e.printStackTrace();
            LogUtils.error("查询流程活动节点信息服务接口异常："+e.getMessage());
        }
        return list;
    }

    /**
     * 查询流程下一个活动节点信息
     */
    public List<String> getWFActivityDefine(String activityID){
        ArrayList<String> list=new ArrayList<String>();
        try {
            LogUtils.info("************************ 查询流程下个获取节点服务 开始*****************************************");
            // 调用服务接口
            String endpointUrl=endpointIP+ ProcessEnum.InquiryNextPossibPartRouteSrv.getName();
            // 调用服务接口
            com.anxin.inquirynextpossibpartroutesrv.OutputParameters response = OSB_BP_SOA_HQ_InquiryNextPossibPartRouteSrvClient.
                    callingInterfaces(activityID, tokenID, endpointUrl);
            // 输出服务调用结果
            System.out.println(response.getBIZSERVICEFLAG());
            System.out.println(response.getBIZRETURNMESSAGE());
            System.out.println(response.getESBFLAG());
            System.out.println(response.getESBRETURNMESSAGE());
            System.out.println(response.getINSTANCEID());

            //保存日志
            OsbProinst osbProinst = new OsbProinst();
            //解析 流程实例ID
            if(response.getOUTPUTCOLLECTION()!=null && response.getOUTPUTCOLLECTION().getOUTPUTCOLLECTIONITEM()!=null
                    && response.getOUTPUTCOLLECTION().getOUTPUTCOLLECTIONITEM().size()>0){
                for (com.anxin.inquirynextpossibpartroutesrv.OUTPUTCOLLECTIONITEM outputcollectionitem : response.getOUTPUTCOLLECTION().getOUTPUTCOLLECTIONITEM()) {
                    if(StringUtils.isNotBlank(outputcollectionitem.getOUT1())){
                        JSONObject jsonObject = XML.toJSONObject(outputcollectionitem.getOUT1());
                        if(jsonObject!=null && jsonObject.get("s:WFActivityDefineList")!=null){
                            JSONObject jsonObject2 = jsonObject.getJSONObject("s:WFActivityDefineList");
                            if(jsonObject2!=null && jsonObject2.get("s:WFActivityDefine")!=null){
                                if(jsonObject2.get("s:WFActivityDefine") instanceof JSONArray){
                                    JSONArray jsonArray = jsonObject2.getJSONArray("s:WFActivityDefine");
                                    for (Object o : jsonArray) {
                                        com.alibaba.fastjson.JSONObject jsonObject3 = com.alibaba.fastjson.JSONObject.parseObject(JSON.toJSONString(o));
                                        if (jsonObject3 != null && jsonObject3.get("s:Id") != null) {
                                            list.add(jsonObject3.get("s:Id").toString());
                                        }
                                    }
                                }else{
                                    JSONObject jsonObject3 = jsonObject2.getJSONObject("s:WFActivityDefine");
                                    if (jsonObject3 != null && jsonObject3.get("s:Id") != null) {
                                        list.add(jsonObject3.get("s:Id").toString());
                                    }
                                }

                            }

                            osbProinst.setDataType(jsonObject.toString());
                        }
                    }
                }
            }
            osbProinst.setEsbFlag(response.getESBFLAG());
            osbProinst.setEsbReturnCode(response.getBIZRETURNCODE());
            osbProinst.setEsbReturnMessage(response.getBIZRETURNMESSAGE());
            osbProinst.setBizServiceFlag(response.getBIZSERVICEFLAG());
            osbProinst.setBizReturnCode(response.getBIZRETURNCODE());
            osbProinst.setInstanceId(response.getINSTANCEID());
            osbProinst.setBizReturnMessage(response.getBIZRETURNMESSAGE());
            osbProinstService.save(osbProinst);
            LogUtils.info("************************ 查询流程下个获取节点服务 结束*****************************************");
        }catch (Exception e){
            e.printStackTrace();
            LogUtils.error("查询流程下个获取节点服务接口异常："+e.getMessage());
        }
        return list;
    }

    /**
     * 指派下一活动节点人员服务
     */
    public String setActivityPar(String processInstId,String userList){
        String msg = "";
        try {
            Map<String, Object> processVar = ActUtils.getProcessVar(processInstId);
            if(processVar!=null && processVar.get("SOA_PROCESSINSTID")!=null){
                List<String> activtiyId = new ArrayList<>();
                List<String> wfActivityInst = this.getWFActivityInst(processInstId);
                for (String s : wfActivityInst) {
                    activtiyId.addAll(this.getWFActivityDefine(s));
                }
                LogUtils.info("************************ 指派下一活动节点人员服务 开始*****************************************");
                // 调用服务接口
                String endpointUrl=endpointIP+ ProcessEnum.ImportActivityParByPIRouteSrv.getName();
                for (String s : activtiyId) {
                    com.anxin.importactivityparbypiroutesrv.OutputParameters response = OSB_BP_SOA_HQ_ImportActivityParByPIRouteSrvClient
                            .callingInterfaces(processVar.get("SOA_PROCESSINSTID").toString(), tokenID, endpointUrl, s, userList);
                    // 输出服务调用结果
                    System.out.println(response.getBIZSERVICEFLAG());
                    System.out.println(response.getBIZRETURNMESSAGE());
                    System.out.println(response.getESBFLAG());
                    System.out.println(response.getESBRETURNMESSAGE());
                    System.out.println(response.getINSTANCEID());
                    msg = response.getBIZSERVICEFLAG();
                    //保存日志
                    OsbProinst osbProinst = new OsbProinst();
                    osbProinst.setEsbFlag(response.getESBFLAG());
                    osbProinst.setEsbReturnCode(response.getBIZRETURNCODE());
                    osbProinst.setEsbReturnMessage(response.getBIZRETURNMESSAGE());
                    osbProinst.setBizServiceFlag(response.getBIZSERVICEFLAG());
                    osbProinst.setBizReturnCode(response.getBIZRETURNCODE());
                    osbProinst.setInstanceId(response.getINSTANCEID());
                    osbProinst.setBizReturnMessage(response.getBIZRETURNMESSAGE());
                    osbProinstService.save(osbProinst);
                }
            }

            LogUtils.info("************************ 指派下一活动节点人员服务 开始*****************************************");
        }catch (Exception e){
            e.printStackTrace();
            LogUtils.error("指派下一活动节点人员服务服务接口异常："+e.getMessage());
        }
       return msg;
    }

}
